﻿/*
 * 选择审核专家。
 * 
 * 2009-5-13
 * 
 * **/


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using EQT.Dal;
using EQT.Model;

public partial class SelectAuditors : System.Web.UI.Page
{
    private DaDept dalDept = new DaDept(DaCommon.DefaultDb);
    private DaEmployee daemp = new DaEmployee(DaCommon.DefaultDb);
    private DaReportAudit dara = new DaReportAudit(DaCommon.DefaultDb);

    private System.Collections.Specialized.StringCollection selectedEID = new System.Collections.Specialized.StringCollection();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)//首次加载
        {
            string report_id=Request.QueryString["report_id"];
            string flow_code = Request.QueryString["flow"];

            if (!String.IsNullOrEmpty(report_id) && !String.IsNullOrEmpty(flow_code))
            {
                ViewState["REPORT_ID"] = report_id;
                ViewState["FLOW_CODE"] = flow_code;

                if (flow_code == SiteConfig.REPORT_AUDIT_FLOW_1_EXPERT_AUDIT)
                {
                    LabelFlow.Text = "选择专家组成员";
                }
                else
                {
                    LabelFlow.Text = "选择站长组成员";
                }

                LabelReportID.Text = report_id;
                System.Collections.Generic.List<MoEmployee> emps = daemp.GetEntities(String.Format("  inner join Report_Audit r on Emp_ID=r.Auditor_ID WHERE r.Report_ID='{0}' AND FLOW_CODE='{1}'", report_id,flow_code));
                if (emps != null && emps.Count > 0)
                {
                    System.Text.StringBuilder sb_eid = new System.Text.StringBuilder();
                    System.Text.StringBuilder sb_ename = new System.Text.StringBuilder();
                    for (int i = 0; i < emps.Count; i++)
                    {
                        sb_eid.Append(emps[i].Emp_Id);
                        sb_ename.Append(emps[i].Fullname);
                        if (i < emps.Count - 1)
                        {
                            sb_eid.Append("/");
                            sb_ename.Append("/");
                        }
                    }
                    ViewState["EMP_IDS"] = sb_eid.ToString();
                    string enames = sb_ename.ToString();
                    ViewState["EMP_NAMES"] = enames;
                    LabelSelectedAuditors.Text = enames;

                }
                else
                {
                    LabelSelectedAuditors.Text = "您未选定任何审批人";
                }

                DataSet dsDept = dalDept.Query(string.Empty);
                Utility.BindDataSet2Combox(dsDept, DDLdept, "Dept_Name", "Dept_ID");
                BindEmps();

            }
            else
            {
                Utility.SiteError("缺失必须的参数:report_id,flow_code");
            }


            
        }

    }


    

    /// <summary>
    /// 单击了选定按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnSelect_Click(object sender, EventArgs e)
    {
        #region 状态历史中已经选定的
        System.Collections.Specialized.StringCollection sc_eid = new System.Collections.Specialized.StringCollection();
        System.Collections.Specialized.StringCollection sc_ename = new System.Collections.Specialized.StringCollection();


        if (ViewState["EMP_IDS"] != null)
        {
            string eids = ViewState["EMP_IDS"].ToString();
            string enames = ViewState["EMP_NAMES"].ToString();

            string[] rc_ids = eids.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            string[] rc_names = enames.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);

            if (rc_ids != null) sc_eid.AddRange(rc_ids);
            if (rc_names != null) sc_ename.AddRange(rc_names);
        }

        #endregion
        
        #region 同步当前页面到已选定
        for (int i = 0; i <= GridViewEmp.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridViewEmp.Rows[i].FindControl("CheckBox1");
            if (cbox != null)
            {

                string eid = GridViewEmp.Rows[i].Cells[1].Text;
                if (cbox.Checked)//选中了
                {                    
                    if (!sc_eid.Contains(eid))
                    {
                        sc_eid.Add(eid);
                        sc_ename.Add(GridViewEmp.Rows[i].Cells[2].Text);
                    }
                 
                }
                else
                {
                    if (sc_eid.Contains(eid))//包含的话，应该清除
                    {
                        sc_ename.RemoveAt(sc_eid.IndexOf(eid));
                        sc_eid.Remove(eid);
                    }

                }
            }
        }
        string[] strlist1 = new string[sc_eid.Count];
        sc_eid.CopyTo(strlist1, 0);
        ViewState["EMP_IDS"] = Utility.ConvertStringListToOneRow(strlist1);

        string[] strlist2 = new string[sc_ename.Count];
        sc_ename.CopyTo(strlist2, 0);
        ViewState["EMP_NAMES"] = Utility.ConvertStringListToOneRow(strlist2);
        LabelSelectedAuditors.Text = ViewState["EMP_NAMES"].ToString();

        #endregion

    }

    /// <summary>
    /// 选择完成，保存最终结果，关闭窗体
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void BtnOK_Click(object sender, EventArgs e)
    {
        //相当于单击的[选定]
        BtnSelect_Click(BtnSelect, e);
        if (ViewState["EMP_IDS"] != null)
        {
            string strlist = (string)ViewState["EMP_IDS"];
            //所有选定的人的id
            string[] ss = strlist.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            string report_id = LabelReportID.Text;

            //将更改保存到数据库
            if (ss != null && ss.Length > 0)
            {
                if (dara.SetReportAuditors(ss, report_id,ViewState["FLOW_CODE"].ToString()) > 0)
                {
                    DaActLog.LogNormalAct(User.Identity.Name, String.Format("设置审批人{0}:{1}", report_id, strlist));
                }
            }
            else
            {
                Utility.GlobalLog.ErrorFormat("选定的专家无效！:{0}.", ViewState["EMP_IDS"]);
            }            

        }
        

        //回发后自动关闭        
        ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "UpdatePanel1", "window.close();", true);

    }


    protected void DDLdept_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindEmps();
    }

    private void BindEmps()
    {
        selectedEID.Clear();
        if (ViewState["EMP_IDS"] != null)
        {
            string strList = (string)ViewState["EMP_IDS"];
            string[] rc_ids = strList.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            selectedEID.AddRange(rc_ids);
        }
        GridViewEmp.DataSource = dara.GetEmp4Select(LabelReportID.Text,ViewState["FLOW_CODE"].ToString() , DDLdept.SelectedValue); 
        GridViewEmp.DataBind();


    }

    /// <summary>
    /// 设定CheckBox1的值
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridViewEmp_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView drv = e.Row.DataItem as DataRowView;
            if (drv != null)
            {
                CheckBox cb = (CheckBox)e.Row.FindControl("CheckBox1");             
                string empid = drv["EMP_ID"].ToString();
                cb.Checked =((int)drv["CHK"] > 0)||(selectedEID.Contains(empid));
            }
        }
    }
}
